/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditNameAuthorFrame.java
 *
 * Created on 24-mei-2011, 13:25:26
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.DomainException;
import com.iceberg.adgt.ui.popup.PopupController;
import com.iceberg.adgt.ui.popup.PopupControllerImpl;

import javax.swing.*;

/**
 * @author Tim Hofman Laptop
 */
public class EditNameAuthorFrame extends javax.swing.JFrame {

	/**
	 * Creates new form EditNameAuthorFrame
	 */
	public EditNameAuthorFrame() {
		super("Edit architecture and structure names and authors");
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		architectureNameTextField = new javax.swing.JTextField();
		architectureAuthorTextField = new javax.swing.JTextField();
		structureNameTextField = new javax.swing.JTextField();
		structureAuthorTextField = new javax.swing.JTextField();
		errorMsg = new javax.swing.JLabel();
		saveButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("Architecture name:");

		jLabel2.setText("Architecture author:");

		jLabel3.setText("Structure name:");

		jLabel4.setText("Structure author:");

		errorMsg.setForeground(java.awt.Color.red);

		saveButton.setText("Save");
		saveButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveButtonActionPerformed(evt);
			}
		});

		cancelButton.setText("Cancel");
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createSequentialGroup()
												.addGap(16, 16, 16)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(errorMsg)
														.addGroup(layout.createSequentialGroup()
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																		.addComponent(jLabel1)
																		.addComponent(jLabel2)
																		.addComponent(jLabel4)
																		.addComponent(jLabel3))
																.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
																		.addComponent(architectureNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
																		.addComponent(architectureAuthorTextField)
																		.addComponent(structureNameTextField)
																		.addComponent(structureAuthorTextField)))))
										.addGroup(layout.createSequentialGroup()
												.addGap(102, 102, 102)
												.addComponent(saveButton)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
												.addComponent(cancelButton)))
								.addContainerGap(18, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jLabel1)
										.addComponent(architectureNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jLabel2)
										.addComponent(architectureAuthorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(structureNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel3))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(structureAuthorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel4))
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createSequentialGroup()
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
												.addComponent(errorMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(layout.createSequentialGroup()
												.addGap(29, 29, 29)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(cancelButton)
														.addComponent(saveButton))))
								.addContainerGap())
		);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

		this.save();
	}//GEN-LAST:event_saveButtonActionPerformed

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed

		this.dispose();
	}//GEN-LAST:event_cancelButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JTextField architectureAuthorTextField;
	private javax.swing.JTextField architectureNameTextField;
	private javax.swing.JButton cancelButton;
	private javax.swing.JLabel errorMsg;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JButton saveButton;
	private javax.swing.JTextField structureAuthorTextField;
	private javax.swing.JTextField structureNameTextField;
	// End of variables declaration//GEN-END:variables
	private JFrame parentFrame;
	private PopupController controller;

	public final void setParentFrame(JFrame frame) {
		this.parentFrame = frame;
	}

	private void save() {
		try {
			this.controller.setArchitectureName(architectureNameTextField.getText());
			this.controller.setArchitectureAuthor(architectureAuthorTextField.getText());
			this.controller.setStructureName(structureNameTextField.getText());
			this.controller.setStructureAuthor(structureAuthorTextField.getText());
			this.dispose();
		} catch (DomainException de) {
			errorMsg.setText(de.getMessage());
		}

	}

	@Override
	public final void dispose() {
		super.dispose();
		this.parentFrame.setEnabled(true);
		this.parentFrame.requestFocus();
	}

	private void init() {
		initComponents();
		this.controller = new PopupControllerImpl();

		this.architectureNameTextField.setText(this.controller.getArchitectureName());
		this.architectureAuthorTextField.setText(this.controller.getArchitectureAuthor());
		this.structureNameTextField.setText(this.controller.getStructureName());
		this.structureAuthorTextField.setText(this.controller.getStructureAuthor());

		this.setVisible(true);
	}
}
